package com.project.server.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.server.common.Result;
import com.project.server.model.AdminUser;
import com.project.server.service.AdminUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/admin/user")
@RequiredArgsConstructor
public class AdminUserController {

    private final AdminUserService adminUserService;

    @GetMapping("/page")
    public Result<Page<AdminUser>> page(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String username) {
        return Result.success(adminUserService.page(pageNum, pageSize, username));
    }

    @PostMapping
    public Result<Void> save(@RequestBody @Validated AdminUser adminUser) {
        adminUserService.save(adminUser);
        return Result.success();
    }

    @PutMapping
    public Result<Void> update(@RequestBody @Validated AdminUser adminUser) {
        adminUserService.update(adminUser);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result<Void> remove(@PathVariable Long id) {
        adminUserService.removeById(id);
        return Result.success();
    }

    @PutMapping("/reset-password/{id}")
    public Result<Void> resetPassword(@PathVariable Long id) {
        adminUserService.resetPassword(id);
        return Result.success();
    }

    @PutMapping("/update-password")
    public Result<Void> updatePassword(
            @RequestParam Long id,
            @RequestParam String oldPassword,
            @RequestParam String newPassword) {
        adminUserService.updatePassword(id, oldPassword, newPassword);
        return Result.success();
    }
}
